﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StorePrgram.Logic;
using StorePrgram.Data;

namespace StorePrgram.View
{
    public partial class MainForm : Form
    {
        private Product product = new Product();
        private HashSet<Product> productsInCurrentTransaction = new HashSet<Product>();

        public MainForm()
        {
            InitializeComponent();
            DisableControls();
            
        }

        public void DisableControls()
        {
            textBoxBarcode.Enabled = false;
            textBoxName.Enabled = false;
            textBoxType.Enabled = false;
            textBoxPrice.Enabled = false;
            buttonAdd.Enabled = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool isBarcodeValid = ProductsDispatcher.IsBarcodeValid(this.textBoxEnterBarcode.Text);

            if (!isBarcodeValid)
            {
                MessageBox.Show("Моля въведете валиден баркод!");
            }
            else
            {
                product = ProductsDispatcher.GetProductByID(
                    Int32.Parse(textBoxEnterBarcode.Text));
                if (product == null)
                {
                    MessageBox.Show("Не съществува продукт с такъв баркод!");
                }
                else
                {
                    this.buttonAdd.Enabled = true;
                    PopulateProductFields(product);
                }

            }

        }

        private void PopulateProductFields(Product product)
        {
            this.textBoxBarcode.Text = product.ProductId.ToString();
            this.textBoxName.Text = product.Name;
            this.textBoxPrice.Text = product.Price;
            this.textBoxType.Text = product.Type.Name;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Product aProduct = new Product();
            int productsCount = this.productsInCurrentTransaction.Count();
            aProduct = this.product;

            bool productExists = ProductExistsCheck(product);
            if (!productExists)
            {
                this.productsInCurrentTransaction.Add(aProduct);
                MessageBox.Show("Един продукт е добавен!");
            }
            else
            {
                MessageBox.Show("Продуктът вече е довавен!");
            }
        }

        private bool ProductExistsCheck(Product product)
        {
            foreach (Product p in productsInCurrentTransaction)
            {
                if (p.ProductId == product.ProductId)
                {
                    return true;
                }
            }
            return false;
        }

        private void buttonEndSale_Click(object sender, EventArgs e)
        {
            if (productsInCurrentTransaction.Count != 0)
            {
                FormSale formSale = new FormSale(productsInCurrentTransaction);

                foreach (Product product in productsInCurrentTransaction)
                {
                    formSale.RichTextBoxProductsInformation.AppendText(
                        "Баркод на продукт: " + product.ProductId + "\r\n"
                        + "Име на продукт: " + product.Name + "\r\n"
                        + "Тип на продукт: " + product.Type.Name + "\r\n"
                        + "Цена: " + product.Price.ToString() + "\r\n"
                        + "----------------------------------------------------------------------" + "\r\n"
                        );
                }
                DialogResult result;
                result = formSale.ShowDialog(this);
                if (result == DialogResult.Cancel)
                {
                    this.productsInCurrentTransaction.Clear();
                    CleanFields();
                }
            }
            else
            {
                MessageBox.Show("Няма добавени продукти!");
            }
        }

        private void CleanFields()
        {
            this.textBoxEnterBarcode.Text = "";
            this.textBoxBarcode.Text = "";
            this.textBoxName.Text = "";
            this.textBoxPrice.Text = "";
            this.textBoxType.Text = "";
            this.buttonAdd.Enabled = false;
        }
    }
}
